home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* libarch.c */
- /* Archive handling for Citadel bulletin board system */
- /************************************************************************/
-
- /************************************************************************/
- /* history */
- /* */
- /* 86Aug01 HAW Created. */
- /************************************************************************/
-
- #include <ctdl.h>
-
- /************************************************************************/
- /* contents */
- /* */
- /* addArchiveList() add new archive to room archive list */
- /* addToList() common for addAr.. and initArch... */
- /* findArchiveName() gets requested archive name */
- /* initArchiveList() eat archive list */
- /************************************************************************/
-
- struct any_list {
- int roomNo;
- char *archName;
- struct any_list *next;
- } Arch_base, Bad_base;
-
- char *ArchFileName = "a:ctdlarch.sys";
- char *ProbFileName = "a:ctdlbad.sys";
-
- extern struct config cfg;
-
- FILE *safeopen();
- /************************************************************************/
- /* findArchiveName() gets archive name for given room */
- /************************************************************************/
- char *findArchiveName(i)
- int i;
- {
- struct any_list *rover;
-
- for (rover = Arch_base.next; rover != NULL; rover = rover->next) {
- if (rover->roomNo == i)
- return rover->archName;
- }
- return NULL;
- }
-
- /************************************************************************/
- /* initArchiveList() set up archive list */
- /************************************************************************/
- initArchiveList()
- {
- struct any_list *rover;
- char name[120], *fgets(), *malloc(), *runner, *b;
- label filename;
- int room;
-
- strCpy(filename, ArchFileName);
- filename[0] += cfg.homeDisk;
- initList(filename, &Arch_base);
- for (rover = Arch_base.next; rover != NULL; rover = rover->next) {
- rover->roomNo = atoi(rover->archName);
- for (runner = rover->archName; isdigit(*runner); runner++)
- ;
- runner++; /* Just over space */
- b = malloc(strLen(runner) + 1);
- strCpy(b, runner);
- free(rover->archName);
- rover->archName = b;
- }
- }
-
- /************************************************************************/
- /* initList() set up list */
- /************************************************************************/
- initList(fileName, base)
- char *fileName;
- struct any_list *base;
- {
- FILE *fd, *safeopen();
- char name[120], *fgets();
- int room;
-
- base->next = NULL;
-
- if ((fd = safeopen(fileName, "r")) == NULL)
- return FALSE;
-
- room = 0;
- while (fgets(name, 118, fd) != NULL) {
- name[strLen(name) - 1] = 0; /* Kill off excess newline */
- addToList(base, room++, name);
- }
-
- fclose(fd);
- return TRUE;
- }
-
- /************************************************************************/
- /* addArchiveList() add to archive list */
- /************************************************************************/
- addArchiveList(room, fn)
- int room;
- char *fn;
- {
- FILE *fd;
- struct any_list *rover;
- char *format = "%d %s\n";
- char replace;
- label filename;
-
- strCpy(filename, ArchFileName);
- filename[0] += cfg.homeDisk;
-
- replace = addToList(room, fn);
-
- if ((fd = safeopen(filename, replace ? "w" : "a")) == NULL) {
- mPrintf("?Couldn't open %s!\n ", filename);
- return FALSE;
- }
-
- if (replace) {
- for (rover = Arch_base.next; rover != NULL; rover = rover->next)
- fprintf(fd, format, rover->roomNo, rover->archName);
- }
- else {
- fprintf(fd, format, room, fn);
- }
-
- fclose(fd);
-
- return TRUE;
- }
-
- /************************************************************************/
- /* addToList() Adds filename to archive list */
- /************************************************************************/
- static int addToList(base, room, fn)
- struct any_list *base;
- int room;
- char *fn;
- {
- struct any_list *rover;
- char found;
-
- for (rover = base, found = FALSE; rover->next != NULL;
- rover = rover->next)
- if (rover->next->roomNo == room) {
- found = TRUE;
- break;
- }
-
- if (!found) {
- rover->next = (struct any_list *) malloc(sizeof Arch_base);
- rover = rover->next;
- rover->next = NULL;
- }
- else {
- rover = rover->next;
- free(rover->archName);
- }
-
- rover->roomNo = room;
- rover->archName = malloc(strLen(fn) + 1);
- strCpy(rover->archName, fn);
-
- return found;
- }
-
- /************************************************************************/
- /* initBadList() read in list of bad filenames */
- /************************************************************************/
- initBadList()
- {
- char filename[30];
-
- strCpy(filename, ProbFileName);
- filename[0] += cfg.homeDisk;
- if (!initList(filename, &Bad_base)) {
- printf("WARNING: Bad filename file ('%s') is missing!\n", filename);
- }
- }
-
- /************************************************************************/
- /* srchBadList() search for given filename in list */
- /************************************************************************/
- srchBadList(fn)
- char *fn;
- {
- char *start, *temp, *strchr(), *strrchr();
- struct any_list *rover;
- label buf;
-
- if (strLen(fn) > 8) return FALSE;
- strCpy(buf, fn);
- if ((start = strrchr(buf, '\\')) == NULL) {
- if ((start = strchr(buf, ':')) == NULL) {
- start = buf;
- }
- else
- start++;
- }
- else
- start++;
- if ((temp = strchr(start, '.')) != NULL) *temp = 0;
- for (rover = Bad_base.next; rover != NULL; rover=rover->next) {
- if (strCmpU(rover->archName, start) == 0) return TRUE;
- }
- return FALSE;
- }
-
- /************************************************************************/
- /* safeopen() Handles DOS bug problem */
- /************************************************************************/
- FILE *safeopen(fn, mode)
- char *fn;
- char *mode;
- {
- FILE *fopen();
-
- if (srchBadList(fn))
- return NULL;
-
- return fopen(fn, mode);
- }
-